package m.jclecai.com.utils.http;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import java.util.List;

import m.jclecai.com.MyApplication;


/**
 * Created by liangzili on 15/8/3.
 */
public class SystemUtils {
	/**
	 * 判断应用是否已经启动
	 *
	 * @param context 一个context
	 * @return boolean
	 */
	public static boolean isAppAlive(Context context) {
		String packageName = "m.jclecai.com";
		ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
		for (int i = 0; i < processInfos.size(); i++) {
			if (processInfos.get(i).processName.equals(packageName)) {
				Log.i("NotificationLaunch", String.format("the %s is running, isAppAlive return true", packageName));
				return true;
			}
		}
		Log.i("NotificationLaunch", String.format("the %s is not running, isAppAlive return false", packageName));
		return false;
	}
	/**
	 *判断当前应用程序处于前台还是后台
	 */
	public static boolean isApplicationBroughtToBackground(final Context context) {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
		if (!tasks.isEmpty()) {
			ComponentName topActivity = tasks.get(0).topActivity;
			if (topActivity.getPackageName().equals(context.getPackageName())) {
				return true;
			}
		}
		return false;
	}

	public static long getCurSysTime(){
		return System.currentTimeMillis();
	}

	/**
	 * 获取版本信息
	 * @return
     */
	public static int getVersionCode(){
		int versionCode = 0;
		Context appContext = MyApplication.getAppContext();
		try {
			PackageInfo packageInfo = appContext.getPackageManager().getPackageInfo(appContext.getPackageName(), 0);
			versionCode = packageInfo.versionCode;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return versionCode;
	}

	/**
	 * 获取版本信息
	 * @return
     */
	public static String getVersionName(){
		String versionName = null;
		Context appContext = MyApplication.getAppContext();
		try {
			PackageInfo packageInfo = appContext.getPackageManager().getPackageInfo(appContext.getPackageName(), 0);
			versionName = packageInfo.versionName;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return versionName;
	}

	/**
	 * 获取当前版本号
	 * @param mContext
	 * @return
	 */
	public static String getCurrentVersionCode(Context mContext){

		try {

			PackageManager manager = mContext.getPackageManager();

			PackageInfo info = manager.getPackageInfo(mContext.getPackageName(),0);

			String version = info.versionName;

			return version;

		} catch (Exception e) {

			e.printStackTrace();

			return  "0.0.0";

		}

	}

	/**
	 * 获取字符
	 *
	 * @param content
	 * @return
	 */
	public static String jsonp2JsonString(String content) {
		if(TextUtils.isEmpty(content))return "";
		content = content.replaceAll("\n","").replaceAll("\r\n","");
		//解析jsonp数据 因为不可预见前面的前缀 所以直接按照特定字符截取
		int index = content.indexOf("{");
		int index2 = content.indexOf("[");
		if (index > index2)
			index = index2;
		if(index > -1)
			content = content.substring(index);

		index = content.lastIndexOf("}");
		index2 = content.lastIndexOf("]");
		if (index < index2)
			index = index2;
		if (index + 1 < content.length())
			content = content.substring(0, index + 1);
		return content;
	}
}
